<?php

namespace App\Http\Controllers\Tools;
use App\Http\Controllers\Controller;

use App\Libs\SmsDayu;
use Illuminate\Http\Request;

class AliDayuController extends Controller {

    public function code(Request $request){
        if(!$request->has('phone')){
            return response()->json('请输入有效的手机号');
        }
        $phone = $request->get('phone');

        if(\Cache::get($phone.':verify:code')){
            return response()->json(['error' => '请一分钟后再试']);
        }
        $code = mt_rand(100000, 999999);
//        $code = '123456';
//        \Cache::put($phone.':verify:code', $code, 1);
//        return response()->json('发送成功');

        $data = SmsDayu::sendSms($phone,'果小哥', ['code' => $code], 'SMS_130210119');
        if($data->Code == 'OK'){
            \Cache::put($phone.':verify:code', $code, 1);
            return response()->json('发送成功');
        } else{
            $msg = '发送失败,稍后再试';
            if($data->Code == 'isv.BUSINESS_LIMIT_CONTROL'){
                $msg = '您今天发送的验证码过多';
            }
            \Log::debug(json_encode($data));
            return response()->json(['error' => $msg]);
        }
    }
}